﻿using Quartz;
/*********************************************************************
 * CopyRight (c) 2009-2014 HangJing Teconology. All Rights Reserved.
 * FileName : 
 * Function : 
 * Created by jijunjian at 2014-11-14 15:24:53.
 * E-Mail   : jijunjian@ihangjing.com
*********************************************************************/
using System;
using Hangjing.DBUtility;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.EleAPIV2;

namespace Hangjing.Job
{
    /// <summary>
    /// 定时刷新token
    /// </summary>
    public class batchRefreshToken : Quartz.IJob
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="context"></param>
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                logger.Info("sbatchRefreshToken任务开始运行");

               
                IList<taobaoAPIAcountInfo> needrefresh = new taobaoAPIAcount().GetList(100, 1, "lasttime < DATEADD(SECOND,-Priority+432000,GETDATE())", "id", 0);
                foreach (var item in needrefresh)
                {
                    ELEAuth auth = new Hangjing.EleAPIV2.ELEAuth(null);
                    access_tokenInfo token = auth.refreshToken(item.Status, item.hovepic);

                    taobaoAPIAcountInfo mm = new taobaoAPIAcountInfo();
                    mm.Parentid = 0;
                    mm.Status = token.shopid;
                    mm.isDel = 0;
                    mm.classname = item.classname;
                    mm.linkurl = item.linkurl.ToString();

                    mm.hovepic = token.refresh_token;
                    mm.pic = token.access_token;
                    mm.Depth = 2;
                    mm.Priority = token.expires_in;
                    mm.lasttime = DateTime.Now;

                    new taobaoAPIAcount().Add(mm);

                }

                logger.Info("sbatchRefreshToken任务运行结束");
            }
            catch (Exception ex)
            {
                logger.Error("batchRefreshToken任务运行运行异常：" + ex);
            }

        }
    }
}
